
package org.linlinjava.litemall.admin.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.storage.StorageService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.service.LitemallStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/storage")
@Validated
public class AdminStorageController {

	@Autowired
	private StorageService storageService;

	@Autowired
	private LitemallStorageService litemallStorageService;

	@RequiresPermissions("admin:storage:list")
	@RequiresPermissionsDesc(menu={"系统管理", "对象存储"}, button="查询")
	@GetMapping("/list")
	public Object list(String key, String name,
		@RequestParam(defaultValue="1") Integer page,
		@RequestParam(defaultValue="10") Integer limit,
		@Sort @RequestParam(defaultValue="add_time") String sort,
		@Order @RequestParam(defaultValue="desc") String order) {
		List<LitemallStorage> storageList = litemallStorageService.querySelective(key, name, page, limit, sort, order);
		return ResponseUtil.okList(storageList);
	}

	@RequiresPermissions("admin:storage:create")
	@RequiresPermissionsDesc(menu={"系统管理", "对象存储"}, button="上传")
	@PostMapping("/create")
	public Object create(@RequestParam("file") MultipartFile file) throws IOException {
		String originalFilename = file.getOriginalFilename();
		LitemallStorage litemallStorage = storageService.store(file.getInputStream(),
			file.getSize(), file.getContentType(), originalFilename);
		return ResponseUtil.ok(litemallStorage);
	}

	@RequiresPermissions("admin:storage:read")
	@RequiresPermissionsDesc(menu={"系统管理", "对象存储"}, button="详情")
	@PostMapping("/read")
	public Object read(@NotNull Integer id) {
		LitemallStorage storageInfo = litemallStorageService.findById(id);
		if (storageInfo == null) {
			return ResponseUtil.badArgumentValue();
		}
		return ResponseUtil.ok(storageInfo);
	}

	@RequiresPermissions("admin:storage:update")
	@RequiresPermissionsDesc(menu={"系统管理", "对象存储"}, button="编辑")
	@PostMapping("/update")
	public Object update(@RequestBody LitemallStorage litemallStorage) {
		if (litemallStorageService.update(litemallStorage) == 0) {
			return ResponseUtil.updatedDataFailed();
		}
		return ResponseUtil.ok(litemallStorage);
	}

	@RequiresPermissions("admin:storage:delete")
	@RequiresPermissionsDesc(menu={"系统管理", "对象存储"}, button="删除")
	@PostMapping("/delete")
	public Object delete(@RequestBody LitemallStorage litemallStorage) {
		String key = litemallStorage.getKey();
		if (StringUtils.isEmpty(key)) {
			return ResponseUtil.badArgument();
		}
		litemallStorageService.deleteByKey(key);
		storageService.delete(key);
		return ResponseUtil.ok();
	}
}